# -*- coding: utf-8 -*-
# @Time : 2021/9/17 14:05
# @Author : jiangping
# @Email : jp@gmail.com
# @File : MRYX_SPFL_001.py
# @Project : mryx_test
"""通过商品分类-在推荐页将商品加入购物车"""
from time import sleep
from appium.webdriver.common.mobileby import MobileBy as By
from appium import webdriver

desired_capabilities = {
    'platformName':'Android',
    'deviceName':'127.0.0.1:62001',
    'platformVersion':'7.1.2',
    'appPackage':'cn.missfresh.application',
    'appActivity':'cn.missfresh.module.main.view.SplashActivity',
    'noReset':True
}
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_capabilities)
sleep(2)
#定位器
#首页
goods_sort_locator = (By.ANDROID_UIAUTOMATOR,'new UiSelector().text("分类")')#首页分类
#分类页
recommend_locator = (By.ANDROID_UIAUTOMATOR,'new UiSelector().text("推荐")')#推荐
#推荐商品页
add1_locator = (By.XPATH,'//androidx.recyclerview.widget.'
                              'RecyclerView[@resource-id=\"cn.missfresh.'
                              'application:id/recycler_view\"]/android.widget.'
                              'RelativeLayout[1]/android.widget.LinearLayout[1]/android.'
                              'widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.'
                              'widget.FrameLayout[1]/android.widget.ImageView[1]')#第一个商品的加号
settlement_locator = (By.ANDROID_UIAUTOMATOR,'new UiSelector().text("去结算(1)")')#去结算按钮
cart_locator = (By.ID,'cn.missfresh.application:id/iv_cart')#购物车图标
determine_locator = (By.ANDROID_UIAUTOMATOR,'new UiSelector().text("确定")')#弹窗的确定按钮
#购物车详情页
del_locator = (By.XPATH,'//android.view.View[@content-desc=\"删除\"]')#删除
#1、点击分类
goods_sort_element = driver.find_element(*goods_sort_locator).click()
sleep(2)
#2、点击推荐
recommend_element = driver.find_element(*recommend_locator).click()
sleep(2)
#3、点击第一个商品右下角的【+】号
n1_element = driver.find_element(*add1_locator).click()
sleep(2)
#断言
print(driver.find_element(*settlement_locator).is_enabled())
#4、清空购物车
# #点击购物车图标
cart_element = driver.find_element(*cart_locator).click()
sleep(2)
#点击删除
del_element = driver.find_element(*del_locator).click()
sleep(2)
#点击确定
determine_element = driver.find_element(*determine_locator).click()
sleep(3)
driver.quit()